package com.zuihai.app.mvp.ui.widget.bottomBar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.zuihai.app.R;
import com.zuihai.app.mvp.ui.utils.MyUtils;

/**
 * Created by YoKeyword on 16/6/3.
 */
public class BottomBarUrlTab extends FrameLayout {
    private ImageView mIcon;
    private TextView mTvTitle;
    private Context mContext;
    private int mTabPosition = -1;
    private String iconOpenSelected;
    private String iconOpenSelect;
    private TextView mTvUnreadCount;

    public BottomBarUrlTab(Context context, @DrawableRes int icon, String title) {
        this(context, null, icon, title);
    }

    public BottomBarUrlTab(Context context, AttributeSet attrs, int icon, String title) {
        this(context, attrs, 0, icon, title);
    }

    public BottomBarUrlTab(Context context, AttributeSet attrs, int defStyleAttr, int icon, String title) {
        super(context, attrs, defStyleAttr);
        //init(context, iconSelectd, iconSelect,title);
    }

    public BottomBarUrlTab(Context context, String iconSelectd, String iconSelect, String title ,int position) {
        super(context);
        init(context, iconSelectd, iconSelect, title,position);

    }

    private void init(Context context, String iconSelecetd, String iconSelect, String title,int position) {
        mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(new int[]{R.attr.selectableItemBackgroundBorderless});
        Drawable drawable = typedArray.getDrawable(0);
        setBackgroundDrawable(drawable);
        typedArray.recycle();
        iconOpenSelected = iconSelecetd;
        iconOpenSelect=iconSelect;

        LinearLayout lLContainer = new LinearLayout(context);
        lLContainer.setOrientation(LinearLayout.VERTICAL);
        lLContainer.setGravity(Gravity.CENTER);
        LayoutParams paramsContainer = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        paramsContainer.gravity = Gravity.CENTER;
        lLContainer.setLayoutParams(paramsContainer);
        if (position!=2) {
            mIcon = new ImageView(context);
            int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(size, size);
            params.setMargins(0,0,0,5);
            mIcon.setLayoutParams(params);
            if (iconOpenSelected.length()>2){
                LogUtils.e("URLTAB NET");
                MyUtils.bottomGlideSelect(getContext(),iconOpenSelect,mIcon,position);
            }else {
                LogUtils.e("URLTAB location "+position);
                switch (position) {
                    case 0:
                        Glide.with(context).load(R.mipmap.button_bottom_home)
                                .into(mIcon);
                        break;
                    case 1:
                        Glide.with(context).load(R.mipmap.button_bottom_find)
                                .into(mIcon);
                        break;
                    case 2:
                        Glide.with(context).load(R.mipmap.button_bottom_release)
                                .into(mIcon);
                        break;
                    case 3:
                        Glide.with(context).load(R.mipmap.button_bottom_friends)
                                .into(mIcon);
                        break;
                    case 4:
                        Glide.with(context).load(R.mipmap.button_bottom_my)
                                .into(mIcon);
                        break;
                }
            }
            //  mIcon.setColorFilter(ContextCompat.getColor(context, R.color.tab_unselect));
            lLContainer.addView(mIcon);
            mTvTitle = new TextView(context);
            mTvTitle.setText(title);
            LinearLayout.LayoutParams paramsTv = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            //paramsTv.topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
            mTvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
            mTvTitle.setTextColor(ContextCompat.getColor(context, R.color.tab_unselect));
            mTvTitle.setLayoutParams(paramsTv);
            lLContainer.addView(mTvTitle);
            addView(lLContainer);
        } else {
            mIcon = new ImageView(context);
            int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 34,
                    getResources().getDisplayMetrics());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    size,size);
            params.setMargins(0,0,0,0);
            mIcon.setLayoutParams(params);

            if (iconOpenSelected.length()>2){
                LogUtils.e("URLTAB NET");
                MyUtils.bottomGlideSelect(getContext(),iconOpenSelect,mIcon,position);
            }else {
                LogUtils.e("URLTAB location 2 "+position);
                Glide.with(context).load(R.mipmap.button_bottom_release)
                        .into(mIcon);
            }
            lLContainer.addView(mIcon);
            mTvTitle = new TextView(context);
            mTvTitle.setText(title);
            LinearLayout.LayoutParams paramsTv = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            paramsTv.topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0, getResources().getDisplayMetrics());
            mTvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
            mTvTitle.setTextColor(ContextCompat.getColor(context, R.color.tab_unselect));
            mTvTitle.setLayoutParams(paramsTv);
            addView(lLContainer);
        }

    }


    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            //LogUtils.e("选中");
            MyUtils.bottomGlideSelected(getContext(),iconOpenSelected,mIcon,mTabPosition);
            mIcon.setSelected(true);
            //mIcon.setColorFilter(ContextCompat.getColor(mContext, R.color.tab_select));
            mTvTitle.setTextColor(ContextCompat.getColor(mContext, R.color.tab_bottom_select));
        } else {
            //LogUtils.e("未选中");
            MyUtils.bottomGlideSelect(getContext(),iconOpenSelect,mIcon,mTabPosition);
            mIcon.setSelected(false);
            //mIcon.setColorFilter(ContextCompat.getColor(mContext, R.color.tab_unselect));
            mTvTitle.setTextColor(ContextCompat.getColor(mContext, R.color.tab_unselect));
        }
    }

    public void setTabPosition(int position) {
        mTabPosition = position;
        if (position == 0) {
            setSelected(true);
        }
    }

    public int getTabPosition() {
        return mTabPosition;
    }

    /**
     * 设置未读数量
     */
    public void setUnreadCount(int num) {
        if (num <= 0) {
            mTvUnreadCount.setText(String.valueOf(0));
            mTvUnreadCount.setVisibility(GONE);
        } else {
            mTvUnreadCount.setVisibility(VISIBLE);
            if (num > 99) {
                mTvUnreadCount.setText("99+");
            } else {
                mTvUnreadCount.setText(String.valueOf(num));
            }
        }
    }

    /**
     * 获取当前未读数量
     */
    public int getUnreadCount() {
        int count = 0;
        if (TextUtils.isEmpty(mTvUnreadCount.getText())) {
            return count;
        }
        if (mTvUnreadCount.getText().toString().equals("99+")) {
            return 99;
        }
        try {
            count = Integer.valueOf(mTvUnreadCount.getText().toString());
        } catch (Exception ignored) {
        }
        return count;
    }

    private int dip2px(Context context, float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }
}
